//
//  CYStatisticsVC.m
//  CreditAddressBook
//
//  Created by Lee on 15/6/25.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "CYStatisticsVC.h"
#import "CYStatisticsView.h"
#import "CYStatisticsContentVC.h"
#import "CYDialTool.h"
#import "CustomIOSAlertView.h"
#import "UMSocial.h"

@interface CYStatisticsVC ()<UIPageViewControllerDataSource, UIPageViewControllerDelegate, UMSocialUIDelegate>
@property (weak, nonatomic) IBOutlet UIView *statisticsView;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentBtn;
@property (strong, nonatomic) NSMutableArray *controllers;
@property (strong, nonatomic) UIPageViewController *pageViewController;
@property (assign, nonatomic) NSInteger selectedIndex;

@end

@implementation CYStatisticsVC

- (UIPageViewController *)pageViewController {
    if (!_pageViewController) {
        _pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
        _pageViewController.delegate = self;
        _pageViewController.dataSource = self;
    }
    return _pageViewController;
}

- (NSMutableArray *)controllers {
    if (!_controllers) {
        _controllers = [NSMutableArray array];
    }
    return _controllers;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"统计";
    
    [self initAttribute];
    
    [self setupShareBtn];
}

- (void)initAttribute {
    for (int i = 0; i < 3; i++) {
        CYStatisticsContentVC *contentVC = [[CYStatisticsContentVC alloc] init];
        contentVC.segmentIndex = i;
        [self.controllers addObject:contentVC];
    }
    
    //添加pageViewController的view到页面显示区域
    [self.statisticsView addSubview:self.pageViewController.view];
    [self.pageViewController.view autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
    [self addChildViewController:self.pageViewController];
    [self.pageViewController didMoveToParentViewController:self];
    
    //默认选中第一个controller
    [self selectedPageViewController:0];
}

- (void)setupShareBtn {
    UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithTitle:@"分享" style:UIBarButtonItemStylePlain target:self action:@selector(share)];
    self.navigationItem.rightBarButtonItem = btn;
}

- (IBAction)segmentClick:(id)sender {
    UISegmentedControl *segment = sender;
    NSInteger index = segment.selectedSegmentIndex;
    [self selectedPageViewController:index];
    
    self.selectedIndex = index;
    
    [self showShareBtn:index];
}

/**
 *  显示分享按钮
 */
- (void)showShareBtn:(NSInteger)index {
    UIBarButtonItem *btn = self.navigationItem.rightBarButtonItem;
    if (index == CYIndexFirst) {
        btn.title = @"分享";
        btn.enabled = YES;
    } else {
        btn.title = @"";
        btn.enabled = NO;
    }
}

- (void)selectedPageViewController:(NSInteger)index {
    UIPageViewControllerNavigationDirection direction = index > [self.controllers indexOfObject:[self.pageViewController.viewControllers lastObject]] ? UIPageViewControllerNavigationDirectionForward : UIPageViewControllerNavigationDirectionReverse;
    
    [self.pageViewController setViewControllers:@[self.controllers[index]] direction:direction animated:YES completion:NULL];
}

#pragma mark - UIPageViewControllerDataSource
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    NSUInteger index = [self.controllers indexOfObject:viewController];
     
    [self showShareBtn:index];
    
    if ((index == NSNotFound) || (index == 0)) {
        return nil;
    }
    
    return self.controllers[--index];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSUInteger index = [self.controllers indexOfObject:viewController];
    
    [self showShareBtn:index];
    
    if ((index == NSNotFound)||(index+1 >= [self.controllers count])) {
        return nil;
    }
    
    return self.controllers[++index];
}

#pragma mark - UIPageViewControllerDelegate
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed {
    if (!completed){
        return;
    }
    NSInteger i = [self.controllers indexOfObject:[pageViewController.viewControllers lastObject]];
    [self.segmentBtn setSelectedSegmentIndex:i];
}

/**
 *  分享
 */
- (void)share {
    CYStatisticsContentVC *controller = self.controllers[self.selectedIndex];
    UIView *shareView = controller.shareView;
    if (shareView) {
        UIImage *shareImage = [CYDialTool imageFromView:shareView];
        
        CGFloat ratio = shareView.width / shareView.height;
        CGFloat height = CYScreeSize.height - 130;
        CustomIOSAlertView *alertView = [[CustomIOSAlertView alloc] init];
        UIView *content = [[UIView alloc] initWithFrame:CGRectMake(0, 3, height * ratio, height)];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:content.frame];
        imageView.image = shareImage;
        [content addSubview:imageView];
        
        alertView.containerView = content;
        alertView.buttonTitles = @[@"取消", @"分享"];
        
        [alertView setOnButtonTouchUpInside:^(CustomIOSAlertView *alertView, int buttonIndex) {
            if (buttonIndex == 1) {
                NSString *shareText = @"天下无赖";//分享内嵌文字
                //调用快速分享接口
                [UMSocialSnsService presentSnsIconSheetView:self appKey:UMAppkey shareText:shareText shareImage:shareImage shareToSnsNames:nil delegate:self];
                [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeImage;
                
            }
        }];
        [alertView show];
        
    }
}

//实现回调方法（可选）：
-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response
{
    //根据`responseCode`得到发送结果,如果分享成功
    if(response.responseCode == UMSResponseCodeSuccess)
    {
        //得到分享到的微博平台名
//        NSLog(@"share to sns name is %@",[[response.data allKeys] objectAtIndex:0]);
    }
}

@end
